package com.olc.railing.utils;

import android.util.Log;

import com.olc.railing.base.BaseApplication;
import com.olc.railing.constant.Constants;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.TimeZone;

public class MyLog {
	public static  final String TAG = "railing_log";
	private static int SDCARD_LOG_FILE_SAVE_DAYS = 5;// sd卡中日志文件的最多保存天数
	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
	public static String deviceId =  "_"+CommUtils.getDeviceSN();
	public static String MYLOGFILEName = deviceId + ".txt";// 本类输出的日志文件名称
	/**
	 * 开关标志位，是否关闭log，如果为false则为开启
	 */
	private static boolean isClosed = false;

	public static void e(String tag, String msg) {
		if (!isClosed ) {
			Log.e(tag, msg);
		//	CrashHandler.SaveLog(tag, msg);
		}
	}
	public static void d(String tag, String msg) {
		if (!isClosed ) {
			Log.d(tag, msg);
			//CrashHandler.SaveLog(tag, msg);
		}
	}
	
	public static void i(String tag, String msg) {
		if (!isClosed ) {
			Log.i(tag, msg);
			//CrashHandler.SaveLog(tag, msg);
		}
	}
	public static void v(String tag, String content) {
		if (!isClosed )
			return;
	//	CrashHandler.SaveLog(tag, content);
	}


	/**
	 * 删除制定的日志文件
	 */
	public static void delLogFile() {// 删除日志文件

		//遍历所有的日志文件，删除3天前的日志文件
		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		Date dateDel = getDateBefore(); //3天前日期

		File fileLog = new File(Constants.FILE_PATH_INFO.get(Constants.FILE_PATH_DIR_LOG));
		if (!fileLog.exists()) {
			fileLog.mkdir();
		}
		File[] files = fileLog.listFiles();
		if (files == null || files.length == 0) {
			return;
		}
		//对文件名进行排序
		Arrays.sort(files, new Comparator<File>() {
			@Override
			public int compare(File o1, File o2) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				try {
					if (o1.getName().length() > 10 && o2.getName().length() > 10) {
						Date date1 = format.parse(o1.getName().substring(0, 10));
						Date date2 = format.parse(o2.getName().substring(0, 10));
						if (date1.before(date2)) {
							return -1;
						} else {
							return 1;
						}
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}

				return 1;
			}
		});


		for (int i = 0; i < files.length; i++) {
//            MyLog.e("MyLog", "fileList Name：" + files[i].getName());
			//删除文件命名不准确的日志
			String fileName = files[i].getName();
			if ((fileName.length() != MYLOGFILEName.length() + 10)) {
				files[i].delete();
				continue;
			}

			String logfileDate = fileName.substring(0, fileName.length() - MYLOGFILEName.length());
//            MyLog.e("MyLog", "current log Name：" + logfileDate);
			try {
				Date dateLog = simpleDateFormat.parse(logfileDate);
				boolean isBefore = dateLog.before(dateDel);
//                MyLog.e("MyLog", "current log is before " + SDCARD_LOG_FILE_SAVE_DAYS + " days：" + isBefore);
				File[] filesNew = fileLog.listFiles(); //重新获取文件的个数，因为删除一次文件的个数就会发生变化
//                MyLog.e("MyLog", "current log num:" + filesNew.length);
				//删掉5天前的Log
				if (isBefore && filesNew.length > 5) {
					File file = new File(Constants.FILE_PATH_INFO.get(Constants.FILE_PATH_DIR_LOG), logfileDate + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR
					if (file.exists()) {
						file.delete();
					}
				}

			} catch (ParseException e) {
				e.printStackTrace();
			}


		}


	}
	/**
	 * 得到现在时间前的几天日期，用来得到需要删除的日志文件名
	 */
	public static Date getDateBefore() {
		Date nowtime = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(nowtime);
		now.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		now.set(Calendar.DATE, now.get(Calendar.DATE) - SDCARD_LOG_FILE_SAVE_DAYS);
		return now.getTime();
	}
}
